package com.wsh.controller.sys;

import com.wsh.dto.sys.DictTypeDto;
import com.wsh.utils.JsonResult;
import com.wsh.base.BaseVo;
import com.wsh.entity.sys.DictTypeEntity;
import com.wsh.sys.DictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author 王思涵
 * @Date 2019/8/10
 * @Version 1.0
 * @Desciption 字典数据类型接口层
 **/
@RestController
@RequestMapping("/sys/dictType")
@Api(tags = "系统：字典类型管理")
public class DictTypeController {
    @Autowired
    private DictTypeService dictTypeService;

    @ApiOperation("获取字典通用方法")
    @PreAuthorize("@el.check('dictType:list')")
    @GetMapping("/list")
    public Object list(DictTypeDto dictTypeDto) {
        return JsonResult.success(dictTypeService.getList(dictTypeDto));
    }

    @ApiOperation("字典类型下拉")
    @GetMapping("/typeList")
    public Object typeList() {
        return JsonResult.success(dictTypeService.list());
    }

    @GetMapping("/getById")
    public Object getById(String formId) {
        return JsonResult.success(new BaseVo(dictTypeService.getById(formId)));
    }

    @ApiOperation("保存字典类型")
    @PreAuthorize("@el.check('dictType:save')")
    @PostMapping("/saveDictType")
    public Object saveDictType(@RequestBody DictTypeEntity dictTypeEntity) {
        return JsonResult.success(dictTypeService.saveOrUpdate(dictTypeEntity));
    }

    @ApiOperation("删除字典类型")
    @PreAuthorize("@el.check('dictType:del')")
    @DeleteMapping("/del")
    public Object delDictType(@RequestParam String formId) {
        try {
            dictTypeService.delDictType(formId);
            return JsonResult.success("删除成功");
        } catch (Exception e) {
            return JsonResult.success(e.getMessage());
        }
    }

}
